/**
 * <p>文件名:		TTT.java</p>
 * <p>版权:		CopyrightTag</p>
 * <p>公司:		CompanyTag</p>
 * @author		周华彬(zhouhuabin@sinoiov.com, zhou_hua_bin@163.com)
 */

package com.caits.lbs.framework.listener;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;

import org.apache.logging.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.web.context.WebApplicationContext;

import com.caits.lbs.framework.log.CommonLogFactory;
import com.caits.lbs.framework.log.CommonSlf4jLogFactory;
import com.caits.lbs.framework.services.BaseService;
import com.caits.lbs.framework.utils.JsonUtils;

/**
 * <p>
 * AbstractSpringContextPostProcessor
 * </p>
 * <p>
 * 初始化spring容器后再执行的业务动作,子类实现initOtherBizWork方法即可.
 * </p>
 * @author 周华彬(zhouhuabin@sinoiov.com, zhou_hua_bin@163.com)
 * @version 0.0.0
 *          <table style="border:1px solid gray;">
 *          <tr>
 *          <th width="100px">版本号</th>
 *          <th width="100px">动作</th>
 *          <th width="100px">修改人</th>
 *          <th width="100px">修改时间</th>
 *          </tr>
 *          <!-- 以 Table 方式书写修改历史 -->
 *          <tr>
 *          <td>0.0.0</td>
 *          <td>创建类</td>
 *          <td>admin</td>
 *          <td>2016年9月26日 下午2:37:15</td>
 *          </tr>
 *          <tr>
 *          <td>XXX</td>
 *          <td>XXX</td>
 *          <td>XXX</td>
 *          <td>XXX</td>
 *          </tr>
 *          </table>
 */
public  class AbstractSpringContextPostListener implements ApplicationListener<ContextRefreshedEvent> {
	/** 变量:TODO,类型:Logger */
	protected final Logger LOG = CommonLogFactory.getLog();
	/** 变量:业务服务map,用来统一定期执行某个动作，类型:Map<String,BaseService> */
	private final Map<String, BaseService> serviceMap = new ConcurrentHashMap<String, BaseService>(50);
	/* (non-Javadoc)
	 * @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
	 */
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		if (event.getApplicationContext().getParent() == null) {// root application context 没有parent，他就是老大.
			// 需要执行的逻辑代码，当spring容器初始化完成后就会执行该方法。
			initOtherBizWork(event.getApplicationContext());
		}
	}
	
	/**
	 * spring初始化完成后 ，再初始化业务工作
	 * @param atx void
	 */
	protected  void initOtherBizWork(ApplicationContext atx) {
		String jdbc_url = (String) CustomizedPropertyConfigurer.getContextProperty("jdbc.url");
		LOG.info("获取到数据库地址,jdbc_url:" + jdbc_url);
		if (atx instanceof WebApplicationContext) {
			WebApplicationContext web = (WebApplicationContext) atx;
			Map<String, BaseService> localMap = atx.getBeansOfType(BaseService.class, true, true);
			LOG.info("获取到baseService列表：" + JsonUtils.getJsonStringFromObject(localMap.keySet()));
			serviceMap.putAll(localMap);
		}
		Executors.newSingleThreadExecutor().execute(new Runnable() {
			@Override
			public void run() {
				while (true) {
					try {
						synchronized (this) {
							wait(60 * 1000);
						}
					} catch (InterruptedException e) {
						LOG.error("线程休眠异常,msg=" + e.getLocalizedMessage(), e);
					}
					try {
							Set<String> keySet = serviceMap.keySet();
							for (String key : keySet) {
								BaseService service = serviceMap.get(key);
								try {
									service.resetParameter();
								} catch (Exception e) {
									LOG.error("执行更新服务异常,服务名sereviceName:" + key, e);
								}
							}
						
					} catch (Exception e) {
						LOG.error("执行定时更新服务参数异常.", e);
					}
				}
			}
		});
		LOG.info("恭喜spring容器所有实例初始化完毕!");
	}
}
